package com.yike.product.controller;

import com.yike.product.param.*;
import com.yike.product.service.GoodsService;
import com.yike.product.vo.*;
import lombok.extern.slf4j.Slf4j;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Add;
import com.yike.common.api.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品 控制器
 *
 * @author yike
 * @since 2024-07-17
 */
@Slf4j
@RestController
@RequestMapping("/goods")
@Api(value = "商品管理API", tags = {"商品管理"})
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 添加商品
     */
    @PostMapping("/add")
    @OperationLog(name = "添加商品", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商品", response = ApiResult.class)
    public ApiResult<Boolean> addGoods(@Validated(Add.class) @RequestBody AddGoodsParam goods) throws Exception {
        boolean flag = goodsService.saveGoods(goods);
        return ApiResult.result(flag);
    }

    /**
     * 修改商品
     */
    @PostMapping("/update")
    @OperationLog(name = "修改商品", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商品", response = ApiResult.class)
    public ApiResult<Boolean> updateGoods(@Validated(Update.class) @RequestBody UpdateGoodsParam goods) throws Exception {
        boolean flag = goodsService.updateGoods(goods);
        return ApiResult.result(flag);
    }

    /**
     * 删除商品
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除商品", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商品", response = ApiResult.class)
    public ApiResult<Boolean> deleteGoods(@PathVariable("id") Long id) throws Exception {
        boolean flag = goodsService.deleteGoods(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/info/{id}")
    //@OperationLog(name = "商品详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商品详情", response = GoodsVo.class)
    public ApiResult<GoodsVo> getGoods(@PathVariable("id") Long id) throws Exception {
        GoodsVo goodsVo = goodsService.getGoods(id);
        return ApiResult.ok(goodsVo);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/customer/{barcode}")
    //@OperationLog(name = "客户端-条码查询商品", type = OperationLogType.INFO)
    @ApiOperation(value = "客户端-条码查询商品", response = GoodsCustomVo.class)
    public ApiResult<GoodsCustomVo> getGoodsByBarcodeForCustomer(@PathVariable("barcode") String barcode) throws Exception {
        GoodsCustomVo goods = goodsService.getByBarcode(barcode);
        return ApiResult.ok(goods);
    }

    /**
     * 获取商品详情
     */
    @PostMapping("/getCustomPageList")
    //@OperationLog(name = "客户端-条码搜索商品", type = OperationLogType.INFO)
    @ApiOperation(value = "客户端-条码搜索商品", response = GoodsCustomVo.class)
    public ApiResult<Paging<GoodsCustomVo>> getGoodsCustomPageList(@Validated @RequestBody GoodsCustomPageParam param) throws Exception {
        Paging<GoodsCustomVo> paging = goodsService.getGoodsCustomPageList(param);
        return ApiResult.ok(paging);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/shop/{barcode}")
    @OperationLog(name = "商户端-条码查询商品", type = OperationLogType.INFO)
    @ApiOperation(value = "商户端-条码查询商品", response = GoodsShopVo.class)
    public ApiResult<GoodsShopVo> getGoodsByBarcodeForShop(@PathVariable("barcode") String barcode) throws Exception {
        GoodsShopVo goods = goodsService.getByBarcodeForShop(barcode);
        return ApiResult.ok(goods);
    }

    /**
     * 商品分页列
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "商品分页", type = OperationLogType.PAGE)
    @ApiOperation(value = "商品分页", response = GoodsVo.class)
    public ApiResult<Paging<GoodsVo>> getGoodsPageList(@Validated @RequestBody GoodsPageParam goodsPageParam) throws Exception {
        Paging<GoodsVo> paging = goodsService.getGoodsPageList(goodsPageParam);
        return ApiResult.ok(paging);
    }

    /**
     * 获取店铺商品分页列表
     */
    @PostMapping("/getShopGoodsList")
    //@OperationLog(name = "获取店铺商品列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "获取店铺商品列表", response = GoodsListVo.class)
    public ApiResult<Paging<GoodsListVo>> getShopGoodsList(@Validated @RequestBody ShopGoodsPageParam shopGoodsPageParam) throws Exception {
        Paging<GoodsListVo> paging = goodsService.getShopGoodsList(shopGoodsPageParam);
        return ApiResult.ok(paging);
    }

    @GetMapping("/getShopGoodsSearchData/{keyword}")
    //@OperationLog(name = "获取店铺商品搜索信息", type = OperationLogType.PAGE)
    @ApiOperation(value = "获取店铺商品搜索信息", response = GoodsSearchVo.class)
    public ApiResult<List<String>> getShopGoodsSearchData(@PathVariable("keyword") String keyword) throws Exception {
        List<String> strList = goodsService.getShopGoodsSearchData(keyword);
        return ApiResult.ok(strList);
    }

}

